package com.mokylin.dbschema.pojo;

import com.mokylin.dbschema.config.ConfigInfo;
import com.mokylin.dbschema.service.DbVersionHeartBeatService;
import com.mokylin.dbschema.util.GlobalUtils;

import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 李朝(Li.Zhao)
 * @since 2015/8/31.
 */

public class DbVersion {

    private int id;

    /**
     * 当前版本号
     */
    private String version;

    /**
     * 数据库状态
     *
     */
    private Status status;

    /**
     * 当前更新数据库的机器mac
     */
    private String buildingServerMac;

    /**
     * 错误描述
     */
    private String desc;


    /**
     * 更新时间
     */
    private Date updateTime;

    public Date getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public String getBuildingServerMac() {
        return buildingServerMac;
    }

    public void setBuildingServerMac(String buildingServerMac) {
        this.buildingServerMac = buildingServerMac;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public Status getStatus() {
        return status;
    }

    public void setStatus(Status status) {
        this.status = status;
    }

    @Override
    public String toString() {
        return "DbVersion{" +
                "version='" + version + '\'' +
                ", status=" + status +
                ", buildingServerMac='" + buildingServerMac + '\'' +
                ", desc='" + desc + '\'' +
                ", updateTime=" + updateTime +
                ", id=" + id +
                '}';
    }

    public static enum Status{
        /**
         * 正常状态
         */
        NORMAL(0),
        /**
         * 准备更新状态
         */
        PREPARED(1),
        /**
         * 正在更新数据库
         */
        BUILDING(2),
        /**
         * 更新异常
         */
        ERROR(-1);

        int val;
        Status(int val){
            this.val = val;
        }

        public int getVal() {
            return val;
        }

        private static final Map<Integer,Status> valMap = new HashMap<>();
        static{
            for(Status e:values()){
                valMap.put(e.val,e);
            }
        }

        public static Status valueOf(int val){
            return valMap.get(val);
        }
    }

    /**
     * db table column name
     */
    public static interface DbColumn{
        String ID = "id";
        String VERSION = "version";
        String STATUS = "status";
        String BUILDING_SERVER_MAC = "building_server_mac";
        String DESC = "desc";
        String UPDATE_TIME = "update_time";
    }

    /**
     * 初始化的DBVersion
     * @return
     */
    public static DbVersion getInitDbVersion(){
        DbVersion  initDbVersion = new DbVersion();
        initDbVersion.setId(1);
        initDbVersion.setVersion("");
        initDbVersion.setBuildingServerMac(GlobalUtils.getServerIdentify());
        initDbVersion.setStatus(DbVersion.Status.NORMAL);
        return initDbVersion;
    }

    /**
     * 更新结束的版本信息
     * @return
     */
    public static DbVersion getBuildEndDbVersion(){
        DbVersion  buildEndDbVersion = new DbVersion();
        buildEndDbVersion.setId(1);
        buildEndDbVersion.setVersion( ConfigInfo.getNewestVersion());
        buildEndDbVersion.setBuildingServerMac(GlobalUtils.getServerIdentify());
        buildEndDbVersion.setStatus(DbVersion.Status.NORMAL);
        buildEndDbVersion.setUpdateTime(new Date());
        return buildEndDbVersion;
    }

}